/*
* Copyright (c) 2016 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
*/
package com.hyyt.shopjsp.util.common;

import com.hyyt.shopjsp.util.json.JSONFormatDate;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

/**
 * 网站前台 请求返回消息对象，适用于js异步加载处理
 *
 * @author ankang
 */
public class ResultMsg {

    private static Logger logger = Logger.getLogger(ResultMsg.class);

    private boolean success;   //成功与否
    private String msg;         //提示消息
    private String data;        //返回json数据


    public static ResultMsg create(boolean success) {
        return new ResultMsg(success);
    }

    public static ResultMsg create(boolean success, String msg) {
        return new ResultMsg(success, msg);
    }

    public static ResultMsg create(boolean success, String msg,String data) {
        return new ResultMsg(success, msg,data);
    }

    public ResultMsg() {
    }

    public ResultMsg(boolean success) {
        this.success = success;
    }

    public ResultMsg(boolean success, String msg) {
        this.success = success;
        this.msg = msg;
    }

    public ResultMsg(boolean success, String msg, String data) {
        this.success = success;
        this.msg = msg;
        this.data = data;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    /**
     * 把传入的map对象，以流的形式写入页面
     * @param object
     * @param response
     */
    public static void create(Object object, HttpServletResponse response) {
        PrintWriter out = null;
        try {
            JsonConfig jsonConfig = new JsonConfig();
            jsonConfig.registerJsonValueProcessor(Date.class, new JSONFormatDate(EnumUtils.DataFormat.Y_M_D_H_m_s.getValue()));
            JSONObject jo = JSONObject.fromObject(object, jsonConfig);//格式化result
            String joStr=jo.toString();
            response.setContentType("text/html; charset=utf-8");
            out = response.getWriter();
            out.println(joStr);
        } catch (IOException e) {
            logger.error("页面取值错误：" + e);
        } finally {
            if (Utils.objectIsNotEmpty(out)) {
                out.flush();
                out.close();
            }
        }
    }

    /**
     * 把传入的map对象，以流的形式写入页面
     * @param objct
     * @param response
     */
    public static void create(Object objct, HttpServletResponse response, boolean isFormatJson) {
        PrintWriter out = null;
        try {
            response.setContentType("text/html; charset=utf-8");
            out = response.getWriter();
            if (isFormatJson) {
                JsonConfig jsonConfig = new JsonConfig();
                jsonConfig.registerJsonValueProcessor(Date.class, new JSONFormatDate(EnumUtils.DataFormat.Y_M_D_H_m_s.getValue()));
                JSONObject jo = JSONObject.fromObject(objct, jsonConfig);//格式化result
                out.println(jo.toString());
            }
            else {
                out.println(objct.toString());
            }
        } catch (IOException e) {
            logger.error("页面取值错误：" + e);
        } finally {
            if (Utils.objectIsNotEmpty(out)) {
                out.flush();
                out.close();
            }
        }
    }
}